package com.xgx.distqcp.Common;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
import com.jfinal.plugin.activerecord.generator.TableMeta;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MyMetaBuilder extends MetaBuilder {
    private  DataSource dataSource;
    public MyMetaBuilder(DataSource dataSource) {
        super(dataSource);
        this.dataSource=dataSource;
    }

    @Override
    protected void buildPrimaryKey(TableMeta tableMeta) throws SQLException {
        ResultSet rs = dbMeta.getPrimaryKeys(conn.getCatalog(), null, tableMeta.name);

        String primaryKey = "";
        int index = 0;
        while (rs.next()) {
            if (index++ > 0) {
                primaryKey += ",";
            }
            primaryKey += rs.getString("COLUMN_NAME");
        }
        if (StrKit.isBlank(primaryKey)) {
            tableMeta.primaryKey = null;
        }
        else {
            tableMeta.primaryKey = primaryKey;
        }
        rs.close();
    }
}
